(function() {
	Path.on("/__basename__.html", function(_current_location) {
		var queryString = _current_location.query;
		var ticket_id = queryString.get("id");
		// ticket有错
		if (!ticket_id) {
			myConfirm("优惠券状态有误！！");
			Path.jump("/main.html");
			return;
		};
		// 获取ticket信息
		coAjax.get(appConfig.bus_ticket_list_by_id + ticket_id, {
			bus_id: busInfo._id
		}, function(result) {
			if (!result.result) {
				myConfirm("优惠券不存在或已过期！！！");
				Path.jump("/main.html");
				return;
			};
			App.set("$Cache.ticket.one", result.result);
		});

		// 获取用户ticket列表
		coAjax.get(appConfig.user.ticket_list, function(result) {
			App.set("$Cache.ticket.user_tickets", JSON.stringify(result.result));
		});

		// 领取优惠券
		App.set("$Event.ticket.add_ticket_to_mine", function() {
			// 领取前判断用户是否登陆
			if (!App.get("loginer")) {
				myConfirm("您还未登陆，请登陆后领取该优惠券。", function() {
					App.set("$Cache.sign_in.cb_url", "/ticket.html?id=" + ticket_id);
					Path.jump("/sign_in.html");
				});
				return;
			};
			var user_tickets = App.get("$Cache.ticket.user_tickets") || "";
			// 判断是否领取过该ticket
			if (user_tickets.indexOf(ticket_id) > -1) {
				myConfirm("您已领取过该优惠券，请勿重复领取");
				return;
			};
			// 添加ticket
			coAjax.post(appConfig.user.ticket_add_from_factory + ticket_id, function(result) {
				if (result.result) {
					myConfirm("领取成功！！！");
					Path.jump("/main.html");
				};
			});
		});
		console.log(queryString.get("id"));
	});
}());